package com.servlet;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.AdminInfo;
import com.dao.AdminDao;
import com.utils.StringUtil;

/**
 * 辅助操作
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	AdminDao ado=new AdminDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String flag=request.getParameter("flag");
		if("time".equals(flag)) {
			time(request, response);
		}else if ("uname".equals(flag)) {
			uname(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	/*
	 * 定时发送时间
	 */
	private void time(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");  //重点在于生成什么格式编码的数据
		response.setContentType("text/html");
		//提出放回session，防止突然掉线
		AdminInfo admin=(AdminInfo)	request.getSession().getAttribute("session_admin");
		request.getSession().setAttribute("session_admin", admin);
		
		LocalDateTime d=LocalDateTime.now();
		DateTimeFormatter df=DateTimeFormatter.ofPattern("yyyy/MM/dd E HH:mm" ); //2018/05/26 星期六 04:25
		response.getWriter().println(df.format(d));
	}
	
	/*
	 * 验证是否重名
	 */
	private void uname(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String adminName=request.getParameter("adminName");
		if(StringUtil.isEmpty(adminName)) {
			response.getWriter().print("2");
		}else {
			Boolean isname=ado.isJuna(adminName);
			if(isname) {
				response.getWriter().print("0");
			}else {
				response.getWriter().print("1");
			}
		}
		
			
	}

}
